package com.ruicar.afs.cloud.account.common.run;

import com.ruicar.afs.cloud.account.common.dto.AccountVoucherInfoDto;
import com.ruicar.afs.cloud.account.common.feign.VoucherManageApiFeign;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.enums.VoucherServiceNameEnum;
import com.ruicar.afs.cloud.common.modules.dto.voucher.VoucherBusinessDataDto;
import com.ruicar.afs.cloud.common.vo.VoucherFlowInfoVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author infinity
 * @version 1.0
 * @date 2020/7/10 16:19
 */
@Slf4j
@Component
@AllArgsConstructor
public class ManageServiceRunner implements VoucherInfoRunner {
    private final VoucherManageApiFeign voucherManageApiFeign;
    private final AbstractVoucherRunner abstractVoucherRunner;

    public IResponse execute(VoucherFlowInfoVo voucherFlowInfo, List<Long> ids) {
        VoucherBean bean = abstractVoucherRunner.getVoucherParam( voucherFlowInfo, ids );
        IResponse<VoucherBusinessDataDto> dto = voucherManageApiFeign.get( bean.getVoucherBusinessDataDto() );
        //通过接口获取数据成功
        List<AccountVoucherInfoDto> dtos = new ArrayList<>();
        if (CommonConstants.SUCCESS.equals( dto.getCode() )) {
            bean.setSourceSystem( AfsEnumUtil.key( VoucherServiceNameEnum.ICOS ) );
            //处理凭证信息
            dtos = abstractVoucherRunner.saveVoucherInfo( dto.getData(), bean );
        } else {//获取失败
            log.error( "凭证获取数据失败，错误原因：[{}]", dto.getMsg() );
            Assert.isTrue( false, MessageFormat.format( "凭证获取数据失败，错误原因：[{0}]", dto.getMsg() ) );
        }
        return IResponse.success( dtos );
    }

}
